package com.anhry.www.safety.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * 2010-04-08
 * remark: 本类主要用于 Calendar于字符串之间的转换
 * author: anhry
 */
public class DateFormater {
	public static String format(String fmt){
		Calendar cal=new GregorianCalendar();
		int year=cal.get(Calendar.YEAR);
		int month=cal.get(Calendar.MONTH)+1;
		int day=cal.get(Calendar.DATE);
		int week=cal.get(Calendar.DAY_OF_WEEK);
		int hour=cal.get(Calendar.HOUR);
		int dhour=cal.get(Calendar.HOUR_OF_DAY);
		int minute=cal.get(Calendar.MINUTE);
		int second=cal.get(Calendar.SECOND);
		String[] weekday={"日","一","二","三","四","五","六"};
		String[] ampm={"AM","PM"};	
		
		if(fmt.indexOf("yyyy")!=-1){
			fmt=fmt.replaceAll("yyyy",String.valueOf(year));
		}
		if(fmt.indexOf("yy")!=-1){
			fmt=fmt.replaceAll("yy",String.valueOf(year).substring(2));
		}
		if(fmt.indexOf("MM")!=-1){			
			fmt=fmt.replaceAll("MM",month<10?"0"+String.valueOf(month):String.valueOf(month));
		}
		if(fmt.indexOf("M")!=-1){
			fmt=fmt.replaceAll("M",String.valueOf(month));
		}
		if(fmt.indexOf("dd")!=-1){
			fmt=fmt.replaceAll("dd",day<10?"0"+String.valueOf(day):String.valueOf(day));
		}
		if(fmt.indexOf("d")!=-1){			
			fmt=fmt.replaceAll("d",String.valueOf(day));
		}
		if(fmt.indexOf("w")!=-1){
			fmt=fmt.replaceAll("w",weekday[week-1]);
		}
		if(fmt.indexOf("HH")!=-1){
			fmt=fmt.replaceAll("HH",dhour<10?"0"+String.valueOf(dhour):String.valueOf(dhour));
		}
		if(fmt.indexOf("H")!=-1){
			fmt=fmt.replaceAll("HH",String.valueOf(dhour));
		}
		if(fmt.indexOf("hh")!=-1){
			fmt=fmt.replaceAll("hh",String.valueOf(hour)+ampm[cal.get(Calendar.AM_PM)]);
		}
		if(fmt.indexOf("h")!=-1){
			fmt=fmt.replaceAll("h",String.valueOf(hour));
		}
		if(fmt.indexOf("mm")!=-1){
			fmt=fmt.replaceAll("mm",minute<10?"0"+String.valueOf(minute):String.valueOf(minute));
		}
		if(fmt.indexOf("m")!=-1){
			fmt=fmt.replaceAll("m",String.valueOf(minute));
		}
		if(fmt.indexOf("ss")!=-1){
			fmt=fmt.replaceAll("s",second<10?"0"+String.valueOf(second):String.valueOf(second));
		}
		if(fmt.indexOf("s")!=-1){
			fmt=fmt.replaceAll("s",String.valueOf(second));
		}		
		return fmt;
	}	
	/**
	 * 函数表述：本函数返回格式化后的日期(不含时间）
	 * 
	 * @param calendar
	 * @return String 格式化后的日期(不含时间），返回为"yyyy-MM-dd"
	 * 
	 */
	public static String formatDate(java.util.Calendar calendar) {
		if (calendar != null) {
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
					"yyyy-MM-dd");
			return simpleDateFormat.format(calendar.getTime());
		} else {
			return "";
		}
	}
	
	/**
	 * 将Calender 转成 Date 类型
	 * 
	 * @param calendar
	 * @return String 格式化后的日期(不含时间），返回为"yyyy-MM-dd"
	 * 
	 */
	public static Date calenderParseDate(java.util.Calendar calendar) {
		try {
			if (calendar != null) {
				SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
						"yyyy-MM-dd");
				String date = simpleDateFormat.format(calendar.getTime());
				return simpleDateFormat.parse(date);
			} 
		} catch (ParseException e) {
			Loggers.error("将Calender 转成 Date 类型:DateFormater:Date calenderParseDate(java.util.Calendar calendar)"+e);
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 函数表述：本函数返回格式化后的日期(不含时间）
	 * 
	 * @param java.util.Date
	 * @return String 格式化后的日期(不含时间），返回为"yyyy-MM-dd"
	 * 
	 */
	public static String formatDate(java.util.Date date) {
		if (date != null) {
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
					"yyyy-MM-dd");
			return simpleDateFormat.format(date);
		} else {
			return "";
		}
	}
	
	/**
	 * 函数表述：本函数返回格式化后的日期(不含时间）
	 * 
	 * @param java.util.Date
	 * @return String 格式化后的日期(不含时间），返回为"yyyy-MM-dd"
	 * 
	 */
	public static Date dateFormatDate(Date date) {
		try {
			if (date != null) {
				SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
						"yyyy-MM-dd");
				String str = simpleDateFormat.format(date);
				return simpleDateFormat.parse(str);
			} 
		} catch (ParseException e) {
			Loggers.error("日期转换异常:DateFormater:public static Date dateFormatDate(Date date)"+e);
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 将字符串转换成日期类型yyyy-MM-dd
	 * @param date string
	 * @return
	 */
	public static Date formatDate(String date) {
		try {
			if (date != null) {
				SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
						"yyyy-MM-dd");
				return simpleDateFormat.parse(date);
			}
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Loggers.error("日期转换异常:DateFormater:public static Date formatDate(String date)"+e);
		}
		return null;
	}
	
	/**
	 * 将字符串转换成日期类型yyyy-MM-dd HH:mm:ss
	 * @param date string
	 * @return
	 */
	public static Date formatDateTime(String date) {
		try {
			if (date != null) {
				SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
						"yyyy-MM-dd HH:mm:ss");
				return simpleDateFormat.parse(date);
			}
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Loggers.error("日期转换异常:DateFormater:public static Date formatDate(String date)"+e);
		}
		return null;
	}

	/**
	 * 将字符串转换成日期类型yyyy-MM-dd HH:mm:ss
	 * @param date string
	 * @return
	 */
	public static String formatDateTime(java.util.Date date) {
		try {
			if (date != null) {
				SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
						"yyyy-MM-dd HH:mm:ss");
				return simpleDateFormat.format(date);
			} 
		} catch (RuntimeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Loggers.error("日期转换异常:DateFormater:public static String formatDateTime(java.util.Date date)"+e);
		}
		return "";
	}
	
	/**
	 * 函数表述：本函数返回格式化后的日期和时间
	 * 
	 * @param calendar
	 * @return String 格式化后的日期和时间,返回为"yyyy-MM-dd HH:mm:ss"
	 * 
	 */
	public static String formatDateTime(java.util.Calendar calendar) {
		try {
			if (calendar != null) {
				SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
						"yyyy-MM-dd HH:mm:ss");
				return simpleDateFormat.format(calendar.getTime());
			}
		} catch (RuntimeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Loggers.error("日期转换异常:DateFormater:public static String formatDateTime(java.util.Calendar calendar)"+e);
		} 
		return "";
	}

	/**
	 * 函数描述：将日期格式化成中国式日期
	 * 
	 * @param calendar
	 * @return String 格式化后的日期和时间,返回为"yyyy年M月d日"
	 */
	public static String formatDateChinese(java.util.Calendar calendar) {
		if (calendar != null) {
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
					"yyyy年M月d日");
			return simpleDateFormat.format(calendar.getTime());
		} else {
			return "";
		}
	}
	/**
	 * 函数描述：将日期格式化成中国式日期
	 * 
	 * @param calendar
	 * @return String 格式化后的日期和时间,返回为"yyyy年M月d日"
	 */
	public static String formatDateChineseOfMonth(java.util.Calendar calendar) {
		if (calendar != null) {
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
			"M月d日");
			return simpleDateFormat.format(calendar.getTime());
		} else {
			return "";
		}
	}

	/**
	 * 函数描述：将日期格式化成中国式日期
	 * 
	 * @param calendar
	 * @return String 格式化后的日期和时间,返回为"yyyy年M月d日,星期E"
	 */
	public static String formatDateChineseWithWeek(java.util.Calendar calendar) {
		if (calendar != null) {
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
					"yyyy年M月d日,E");
			return simpleDateFormat.format(calendar.getTime());
		} else {
			return "";
		}
	}

	/**
	 * 函数表述：本函数返回格式化后的中国式日期和时间
	 * 
	 * @param calendar
	 *            格式化后的日期和时间,返回为"yyyy年M月d日 HH时mm分ss秒,星期E"
	 * @return
	 */
	public static String formatDateTimeChinese(java.util.Calendar calendar) {
		if (calendar != null) {
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
					"yyyy年M月d日 HH时mm分ss秒,E");
			return simpleDateFormat.format(calendar.getTime());
		} else {
			return "";
		}
	}

	/**
	 * 只返回月份和天
	 * 
	 * @param calendar
	 * @return
	 */
	public static String formatDateToMonthDay(java.util.Calendar calendar) {
		if (calendar != null) {
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat("M.d");
			return simpleDateFormat.format(calendar.getTime());
		} else {
			return "";
		}
	}

	/**
	 * 获取指定月份指定周日期数据
	 * 
	 * @param curTime
	 * @param week
	 * @return
	 */
	public static Calendar[] getTheWeek(Calendar curTime, int week) {
		Calendar[] cals = new Calendar[7];
		curTime.set(GregorianCalendar.WEEK_OF_MONTH, week);
		curTime.set(GregorianCalendar.DAY_OF_WEEK, GregorianCalendar.MONDAY);
		for (int i = 0; i < cals.length; i++) {
			cals[i] = (Calendar) curTime.clone();
			curTime.add(GregorianCalendar.DAY_OF_MONTH, 1);
		}
		return cals;
	}
	
	 public static Calendar[] getWeek(int year, int week) { 
		 Calendar[] cals = new Calendar[7];
		 Calendar c = new GregorianCalendar(); 
		 c.set(Calendar.YEAR, year); 
		 c.set (Calendar.MONTH, Calendar.JANUARY); 
		 c.set(Calendar.DATE, 3);
		 
		 Calendar cal = (GregorianCalendar) c.clone(); 
		 cal.add(Calendar.DATE, week * 7);
		 for (int i = 0; i < cals.length; i++) {
			cals[i] = (Calendar) cal.clone();
			cal.add(GregorianCalendar.DAY_OF_YEAR, 1);
		 }
		 return cals; 
		 }

	/**
	 * 获得当前季度
	 * @param month
	 * @return
	 */
	public static int getQuarter(Calendar ca) {
		if(ca != null){
			int month = ca.get(Calendar.MONTH);
			if (month < 3) {
				return 1;
			} else if (month < 6) {
				return 2;
			} else if (month < 9) {
				return 3;
			} else {
				return 4;
			}
		}
		return 0;
	}
	/**
	 * 获得当前年和季度
	 * @param month
	 * @return
	 */
	public static String getQuarter(Date date) {
		Calendar ca = Calendar.getInstance();
		ca.setTime(date);
		ca.add(Calendar.MONTH, +1);	//月份加1
		int month = ca.get(Calendar.MONTH);
		int year = ca.get(Calendar.YEAR);
//		System.out.println(month);
		if (month >= 1 && month <= 3) {
			return year+"第1季度";
		} else if (month >= 4 && month <= 6) {
			return year+"第2季度";
		} else if (month >= 7 && month <= 9) {
			return year+"第3季度";
		} else {
			return year+"第4季度";
		}
	}
	
	public static int getDaysOfMonth(int year,int month){
		Calendar   cal   =   Calendar.getInstance();   
		cal.set(Calendar.YEAR,year); //指定年  
		cal.set(Calendar.MONTH,month-1);//指定月，应该是指定月-1，  
		int   maxDate   =   cal.getActualMaximum(Calendar.DATE);
		return maxDate;
	}
	
	public static Calendar[] getTheMonth(int year,int month){
		Calendar[] cals = new Calendar[getDaysOfMonth(year, month)];
		Calendar c = new GregorianCalendar(); 
		c.set(Calendar.YEAR, year); 
		c.set (Calendar.MONTH, month-1);
		c.set(Calendar.DATE, 1);
		for (int i = 0; i < cals.length; i++) {
			cals[i] = (Calendar) c.clone();
			c.add(GregorianCalendar.DAY_OF_YEAR, 1);
		}
		return cals;
	}
	
	public static String getWeekByDate(Date date){
		final String dayNames[] = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五","星期六" };
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK)-1;
//		if(dayOfWeek <0)dayOfWeek=0;
		return dayNames[dayOfWeek];
	}

	
	public static void main(String[] args) {
//		Calendar[] cals = getTheMonth(2011, 2);
//		for (int i = 0; i < cals.length; i++) {
//			System.out.println(formatDateChinese(cals[i]));
//		}
		System.out.println(getWeekByDate(new Date()));
//		System.out.println(getQuarter(Calendar.getInstance()));
//		System.out.println(getQuarter(new Date()));
//		System.out.println(getDaysOfMonth(2011, 9));
	}
}

